<template>
  <div class="app-container">
    <avue-crud
      ref="crud"
      :page="page"
      :data="dataList"
      :table-loading="dataListLoading"
      :option="tableOption"
      @search-change="searchChange"
      @on-load="getDataList"
      @refresh-change="refreshChange"
    >
      <template slot="menuLeft">
        <el-button
          type="primary"
          size="small"
          icon="el-icon-plus"
          @click="addOrUpdateHandle()"
        >新增</el-button>
      </template>

      <template
        slot="status"
        slot-scope="scope"
      >
        <el-tag
          v-if="scope.row.status === 0"
          size="small"
          type="danger"
        >禁用</el-tag>
        <el-tag
          v-else
          size="small"
        >正常</el-tag>
      </template>

      <template slot="style" slot-scope="scope">
        <el-tag v-if="scope.row.style === 1" size="small">一列一个</el-tag>
        <el-tag v-if="scope.row.style === 2" size="small">一列两个</el-tag>
        <el-tag v-if="scope.row.style === 3" size="small">一列三个</el-tag>
      </template>

      <template
        slot="menu"
        slot-scope="scope"
      >
        <!--          v-if="isAuth('prod:prodTag:update')"-->
        <!--          v-if="isAuth('prod:prodTag:delete')"-->
        <el-button
          type="primary"
          size="small"
          icon="el-icon-edit"
          @click="addOrUpdateHandle(scope.row.id)"
        >修改</el-button>
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="small"
          @click.stop="deleteHandle(scope.row.id)"
        >删除</el-button>
      </template>

    </avue-crud>
    <add-or-update
      v-if="addOrUpdateVisible"
      ref="addOrUpdate"
      @refreshDataList="refreshChange"
    />
  </div>
</template>

<style>

</style>

<script>
import { tableOption } from '@/crud/mall/prodGroup'
import { getPageList } from '@/api/mall/prodGroup'
import AddOrUpdate from './add-or-update'
export default {
  components: {
    AddOrUpdate
  },
  data() {
    return {
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 10 // 每页显示多少条
      },
      dataList: [],
      dataListLoading: false,
      tableOption: tableOption,
      addOrUpdateVisible: false
    }
  },
  methods: {
    getDataList: function(page, params,done) {
      this.dataListLoading = true
      getPageList(Object.assign(
        {
          current: page == null ? this.page.currentPage : page.currentPage,
          size: page == null ? this.page.pageSize : page.pageSize
        },
        params
      )).then(res => {
        this.dataList = res.records
        this.page.total = res.total
        this.dataListLoading = false
        if(done){
          done()
        }
      })
    },
    searchChange: function(params,done) {
      this.getDataList(this.page, params,done)
    },
    refreshChange: function() { // 刷新回调
      this.getDataList(this.page)
    },
    addOrUpdateHandle: function(id) { // 新增 或修改
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    }
  }
}
</script>
